const requireDirectory = require('require-directory')
const Router = require('koa-router')

class InitManager{
  static init(app){
    InitManager.app = app
    InitManager.initLoadRouter()
    InitManager.initResponse()
  }
  //动态注册中间件
  static initLoadRouter(){
    requireDirectory(module, '../app/api',{
      visit: moduleItem => {
        if(moduleItem instanceof Router){
          InitManager.app.use(moduleItem.routes())
        }
      }
    })
  }

  //定义status为200时返回的数据格式
  static initResponse(){
    global.responseData = function(data=null, message='success', code='200'){
      return {
        data,
        message,
        code
      }
    }
  }
}

module.exports = InitManager

